<?php

/*
 * --------------------------------
 * 功能:HTTP接口 发送短信
 * 修改日期: 2009-04-08
 * 说明: http://http.yunsms.cn/tx/?uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容
 * 状态:
 * 100 发送成功
 * 101 验证失败
 * 102 短信不足
 * 103 操作失败
 * 104 非法字符
 * 105 内容过多
 * 106 号码过多
 * 107 频率过快
 * 108 号码内容空
 * 109 账号冻结
 * 110 禁止频繁单条发送
 * 111 系统暂定发送
 * 112 有错误号码
 * 113 定时时间不对
 * 114 账号被锁，10分钟后登录
 * 115 连接失败
 * 116 禁止接口发送
 * 117 绑定IP不正确
 * 120 系统升级
 * --------------------------------
 */
function sendsms ($mobile, $content)
{
	return sendsms_imtong($mobile, $content);
}

function sendsms_imtong ($mobile, $content)
{
	// 过滤黑字典
	$content = str_replace('1989', '1 9 8 9', $content);
	$content = str_replace('1259', '1 2 5 9', $content);
	$content = str_replace('12590', '1 2 5 9 0', $content);
	$content = str_replace('10086', '1 0 0 8 6', $content);
	
	// 配置信息
	$smsapi = "api.chanyoo.cn"; // 短信网关
	$charset = "utf8"; // 文件编码
	
	$content = $content . "【" . $GLOBALS['_CFG']['shop_name'] . "】";
	
	$sendurl = "http://" . $smsapi . "/" . $charset . "/interface/send_sms.aspx?username=" . $GLOBALS['_CFG']['ecsdxt_user_name'] . "&password=" . $GLOBALS['_CFG']['ecsdxt_pass_word'] . "&receiver=" . $mobile . "&content=" . urlencode($content) . "";
	
	$result = file_get_contents($sendurl);
	
	$xml = simplexml_load_string($result);
	
	if($xml->result >= 0)
	{
		return true;
	}
	else
	{
		return $xml->message;
	}
}

function ismobile ($mobile)
{
	return (strlen($mobile) == 11 || strlen($mobile) == 12) && (preg_match("/^13\d{9}$/", $mobile) || preg_match("/^14\d{9}$/", $mobile) || preg_match("/^15\d{9}$/", $mobile) || preg_match("/^18\d{9}$/", $mobile) || preg_match("/^0\d{10}$/", $mobile) || preg_match("/^0\d{11}$/", $mobile));
}

function getverifycode ()
{
	$verifycode = rand(100000, 999999);
	
	$verifycode = str_replace('1989', '9819', $verifycode);
	$verifycode = str_replace('1259', '9521', $verifycode);
	$verifycode = str_replace('12590', '09521', $verifycode);
	$verifycode = str_replace('10086', '68001', $verifycode);
	
	return $verifycode;
}

function httprequest ($url, $data = array(), $abort = false)
{
	if(! function_exists('curl_init'))
	{
		return empty($data) ? doget($url) : dopost($url, $data);
	}
	$timeout = $abort ? 1 : 2;
	$ch = curl_init();
	if(is_array($data) && $data)
	{
		$formdata = http_build_query($data);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $formdata);
	}
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	$result = curl_exec($ch);
	return (false === $result && false == $abort) ? (empty($data) ? doget($url) : dopost($url, $data)) : $result;
}

function doget ($url)
{
	$url2 = parse_url($url);
	$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
	$url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);
	$host_ip = @gethostbyname($url2["host"]);
	$fsock_timeout = 2; // 2 second
	if(($fsock = fsockopen($host_ip, $url2['port'], $errno, $errstr, $fsock_timeout)) < 0)
	{
		return false;
	}
	$request = $url2["path"] . ($url2["query"] ? "?" . $url2["query"] : "");
	$in = "GET " . $request . " HTTP/1.0\r\n";
	$in .= "Accept: */*\r\n";
	$in .= "User-Agent: Payb-Agent\r\n";
	$in .= "Host: " . $url2["host"] . "\r\n";
	$in .= "Connection: Close\r\n\r\n";
	if(! @fwrite($fsock, $in, strlen($in)))
	{
		fclose($fsock);
		return false;
	}
	return gethttpcontent($fsock);
}

function dopost ($url, $post_data = array())
{
	$url2 = parse_url($url);
	$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
	$url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);
	$host_ip = @gethostbyname($url2["host"]);
	$fsock_timeout = 2; // 2 second
	if(($fsock = fsockopen($host_ip, $url2['port'], $errno, $errstr, $fsock_timeout)) < 0)
	{
		return false;
	}
	$request = $url2["path"] . ($url2["query"] ? "?" . $url2["query"] : "");
	$post_data2 = http_build_query($post_data);
	$in = "POST " . $request . " HTTP/1.0\r\n";
	$in .= "Accept: */*\r\n";
	$in .= "Host: " . $url2["host"] . "\r\n";
	$in .= "User-Agent: Lowell-Agent\r\n";
	$in .= "Content-type: application/x-www-form-urlencoded\r\n";
	$in .= "Content-Length: " . strlen($post_data2) . "\r\n";
	$in .= "Connection: Close\r\n\r\n";
	$in .= $post_data2 . "\r\n\r\n";
	unset($post_data2);
	if(! @fwrite($fsock, $in, strlen($in)))
	{
		fclose($fsock);
		return false;
	}
	return gethttpcontent($fsock);
}

function gethttpcontent ($fsock = null)
{
	$out = null;
	while($buff = @fgets($fsock, 2048))
	{
		$out .= $buff;
	}
	fclose($fsock);
	$pos = strpos($out, "\r\n\r\n");
	$head = substr($out, 0, $pos); // http head
	$status = substr($head, 0, strpos($head, "\r\n")); // http status line
	$body = substr($out, $pos + 4, strlen($out) - ($pos + 4)); // page body
	if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches))
	{
		if(intval($matches[1]) / 100 == 2)
		{
			return $body;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

?>